package com.namo.cchapi.consumer;

import com.namo.cchapi.entity.PicCaptchaEntity;
import com.namo.cchapi.service.MessageCaptchaService;
import com.namo.cchapi.service.PicCaptchaService;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author WangXin
 * @date 2021/4/12 16:38
 */
@Component
public class MessageConsumer {
//    @Autowired
//    MessageCaptchaService messageCaptchaService;
//
//
//    @Bean
//    public void defaultMQPushConsumer() throws MQClientException {
//        // 实例化消费者
//        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("cch_api_consumer");
//
//        // 设置NameServer的地址
//        consumer.setNamesrvAddr("192.168.99.99:9876");
//        // 订阅一个或者多个Topic，以及Tag来过滤需要消费的消息
//        consumer.subscribe("cch_api", "message");
//        // 注册回调实现类来处理从broker拉取回来的消息
//        consumer.registerMessageListener(new MessageListenerConcurrently() {
//            @Override
//            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
//                try {
//                    String s = messageCaptchaService.sendMessage(new String(msgs.get(0).getBody(), StandardCharsets.UTF_8));
//                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
//                }
////
////                System.out.println(new String(msgs.get(0).getBody(), StandardCharsets.UTF_8));
////                System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
//                // 标记该消息已经被成功消费,否则会被其它消费
//
//            }
//        });
//        // 启动消费者实例
//        consumer.start();
//        System.out.printf("Consumer Started.%n");
//    }
}
